class QA:
    def __init__(self):
        pass
    def matchArticle(self, conditions):
        cql='''match (c:City)-[rc:RelatedCity]-(a:Article)-[rv:RelatedVegetable]-(v:Vegetable) '''
        multi_condition = False
        if 'veg' in conditions.keys():
            prefix = 'and ' if multi_condition else 'where '
            cql+=prefix
            cql+="v.name = '%s' "%conditions['veg']
            multi_condition=True
        if 'city' in conditions.keys():
            prefix = 'and ' if multi_condition else 'where '
            cql+=prefix
            cql+="c.name contains '%s' "%conditions['city']
            multi_condition=True

        if 'ind' in conditions.keys():
            prefix = 'and ' if multi_condition else 'where '
            cql+=prefix
            cql+="a.indicator = '%s' "%conditions['ind']
            multi_condition=True

        cql += "with a,c,v,rc,rv match (a)-[ra:RelatedArticle]-(e:Event) return a,c,e,v,rc,rv,ra limit 200"
        return cql
    def matchPrice(self, conditions):
        sql = '''select * from price '''
        multi_condition = False
        if 'veg' in conditions.keys():
            prefix = 'and ' if multi_condition else 'where '
            sql+=prefix
            sql+="veg = '%s' "%conditions['veg']
            multi_condition=True
        if 'city' in conditions.keys():
            prefix = 'and ' if multi_condition else 'where '
            sql+=prefix
            sql+="city like '%s_' "%conditions['city']
            multi_condition=True
        sql += 'order by date ;'
        return sql

    def toDict(self, df):
        datas = {}
        for col in df.columns:
            datas[col] = list(set(df[col]))
        ret = {}
        # for a in datas['a']:
        #     tempDict = dict(a.items())
        #     tempDict['id'] = a.identity
        #     tempDict['label'] = list(a.labels)[0] if len(list(a.labels))>0 else None
        #     ret['articles'].append(tempDict)
        for col in ['v','c','e','a']:
            tempList = []
            for a in datas[col]:
                tempDict = dict(a.items())
                tempDict['id'] = a.identity
                tempDict['label'] = list(a.labels)[0] if len(list(a.labels)) > 0 else 'None'
                tempList.append(tempDict)
            ret[col] = tempList
        for col in ['rv','rc','ra']:
            tempList = []
            for a in datas[col]:
                tempDict = {}
                tempDict['id'] = a.identity
                tempDict['start_node'] = a.start_node.identity
                tempDict['end_node'] = a.end_node.identity
                tempDict['type'] = list(a.types())[0] if len(list(a.types())) > 0 else 'None'
                tempList.append(tempDict)
            ret[col]=tempList
        print(ret)
        return ret